Vue.prototype.$http = axios;
//每次发送axios请求之前都会加上这个基础路径
axios.defaults.baseURL = "http://localhost:8080"

axios.interceptors.request.use(res=>{
    let token = localStorage.getItem("token");
    if(token){
        res.headers["token"] = token;
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的前置拦截器【拦截调用后端的请求】====================//

//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
    //后端响应的是没有登录的信息
    if (false === res.data.success && "noLogin" === res.data.message ||
        (false === res.data.success&& "timeout" === res.data.message)) {
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="/login.html"
    }
    return res;
},error => {
    Promise.reject(error)
})
//======================axios的后置拦截器【处理后台登录拦截的结果】====================//
let url = location.href;//获取当前链接
let path = url.substring(url.lastIndexOf("/"))
if (path!="/login.html" && path !="/register.html"
    &&(path.indexOf("/binder.html"))==-1 && (path.indexOf("/callback.html"))==-1){
    let logininfo = localStorage.getItem("logininfo")
    if (!logininfo){
        location.href="/login.html"
    }
}

function parseUrlParams2Obj(url){//xxxx?code=111&key=222
    var obj = {}
    //将请求地址分割
    let params = url.substring(url.indexOf("?")+1);//code=111&key=222
    //将参数分割
    let paramArr = params.split("&");//{code=111,key=222}
    for (var a = 0;a < paramArr.length;a++){
        let paraArr = paramArr[a].split("=");//code,111
        obj[paraArr[0]]=paraArr[1];
    }
    return obj


}